/**
 * getRepairerList   获取维修人员列表
 * queryRepairerList 查询维修人员
 * addRepairer       添加维修人员
 * getInfoRepairer   获取维修人员信息
 * updateRepairer    修改维修人员信息
 * lockRepairer      锁定维修人员账号
 * deleteRepairer    删除维修人员
 */
// 导入密码加密
const bcrypt = require("bcryptjs")

const repairStatement = require("../../common/sql_sentences/repair")
const repairerStatement = require("../../common/sql_sentences/repairer")

exports.getRepairerList = async (req, res) => {
  try {
    const { type_id, page, pagesize } = req.query
    // 获取维修人员总数
    let total,
      sqlStr = repairerStatement.selectCount({
        type_id
      })

    let results = await RunSQL(sqlStr)
    total = results[0].total

    // 获取维修人员列表
    sqlStr = repairerStatement.selectLimit({ type_id }, page, pagesize)
    results = await RunSQL(sqlStr)
    return res.successSend("", 10000, { row: results, total })
  } catch (error) {
    res.errSend(error)
  }
}

exports.screenRepairerList = async (req, res) => {
  try {
    // 获取维修人员列表
    sqlStr = `select job_number, name, hex(type_id) type_id from repairer`
    results = await RunSQL(sqlStr)
    return res.successSend("", 10000, results)
  } catch (error) {
    res.errSend(error)
  }
}

exports.queryRepairerList = async (req, res) => {
  try {
    // 查找维修人员
    const sqlStr = repairerStatement.select(req.query)

    let results = await RunSQL(sqlStr)
    if (results.length !== 1) return res.successSend("没有找到维修人员！", 10005)
    return res.successSend("查找成功！", 10000, results[0])
  } catch (error) {
    res.errSend(error)
  }
}

exports.getInfoRepairer = async (req, res) => {
  try {
    // 获取维修人员信息
    const sqlStr = repairerStatement.idSelect(req.params.id)
    let results = await RunSQL(sqlStr)
    if (results.length !== 1) return res.successSend("获取维修人员信息失败！", 10005)
    return res.successSend("", 10000, results[0])
  } catch (error) {
    res.errSend(error)
  }
}

exports.addRepairer = async (req, res) => {
  try {
    const data = req.body
    // 判断工号是否存在
    let sqlStr = repairerStatement.selectCount({ job_number: data.job_number })
    let results = await RunSQL(sqlStr)
    if (results[0].total) return res.errSend("工号已存在！", 10005)

    // 判断手机号是否存在
    if (data.phone) {
      sqlStr = repairerStatement.selectPhone(data.phone)
      results = await RunSQL(sqlStr)
      if (results.length) return res.errSend("手机号已存在！", 10005)
    }

    // 添加维修人员
    sqlStr = repairerStatement.insert({
      ...data,
      state: 0,
      password: bcrypt.hashSync(data.password, 10),
    })
    results = await RunSQL(sqlStr)
    if (results.affectedRows !== 1) return res.successSend("添加维修人员失败！", 10005)

    return res.successSend("添加维修人员成功！")
  } catch (error) {
    res.errSend(error)
  }
}

exports.updateRepairer = async (req, res) => {
  try {
    const data = req.body

    // 判断手机号是否存在
    if (data.phone) {
      sqlStr = repairerStatement.selectPhone(data.phone)
      results = await RunSQL
      if (results[0]?.id && results[0]?.id !== data.id)
        return res.errSend("手机号已存在！", 10005)
    }

    // 修改维修人员信息
    sqlStr = repairerStatement.idUpdate(data, data.id)
    results = await RunSQL(sqlStr)
    if (results.affectedRows !== 1)
      return res.successSend("修改维修人员信息失败！", 10005)
    return res.successSend("修改维修人员信息成功！")
  } catch (error) {
    res.errSend(error)
  }
}

exports.lockRepairer = async (req, res) => {
  try {
    // 锁定/解锁维修人员账号
    const data = req.body
    if (req.auth.id !== data.id) {
      const sqlStr = repairerStatement.idUpdate({ state: data.idLock ? 0 : 1 }, data.id)
      let results = await RunSQL(sqlStr)

      if (results.affectedRows !== 1)
        return res.successSend("锁定/解锁维修人员失败！", 10005)
      return res.successSend("锁定/解锁维修人员成功！")
    }
  } catch (error) {
    res.errSend(error)
  }
}

exports.deleteRepairer = async (req, res) => {
  const id = req.params.id
  try {
    // 获取维修人员信息
    let sqlStr = repairerStatement.idSelect(id)
    let results = await RunSQL(sqlStr)
    if (!results.length) return res.successSend("获取维修人员信息失败！", 10005)

    // 获取报修单总数
    sqlStr = repairStatement.selectCount(query)
    results = await RunSQL(sqlStr)
    if (results[0].total) return res.successSend("删除维修人员失败！有报修单属于该维修人员！", 10005)

    // 删除维修人员
    sqlStr = repairerStatement.delete(id)
    results = await RunSQL(sqlStr)
    if (results.affectedRows !== 1) return res.successSend("删除维修人员失败！", 10005)
    return res.successSend("删除维修人员成功！")
  } catch (error) {
    res.errSend(error)
  }
}
